package org.yaac.server.egql.evaluator.function;

import org.yaac.server.egql.evaluator.EvaluationResult;
import org.yaac.server.egql.exception.EGQLException;
import org.yaac.server.egql.processor.ProcessData.ProcessDataRecord;
import org.yaac.shared.ErrorCode;

/**
 * @author Max Zhu (thebbsky@gmail.com)
 *
 */
public class LCaseFunction extends Function {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	@Override
	public void validate() throws EGQLException {
		FunctionUtil.ensureParamSize(ops, 1);
	}

	@Override
	public EvaluationResult evaluate(ProcessDataRecord record) {
		if (this.ops.isEmpty()) {
			return new EvaluationResult(null).withWarning(ErrorCode.W102);
		} else if (this.ops.size() == 1) {
			EvaluationResult r = ops.get(0).evaluate(record);
			if (r.getPayload() instanceof String) {
				return new EvaluationResult(((String)r.getPayload()).toLowerCase(), r);
			} else {
				return r.withWarning(ErrorCode.W104);
			}
		} else {
			return this.ops.get(0).evaluate(record).withWarning(ErrorCode.W102);
		}
	}
}
